package com.ztf0337.mycurriculumdesign.ui.fragment;

import static android.view.View.GONE;
import static android.view.View.VISIBLE;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.ztf0337.mycurriculumdesign.R;
import com.ztf0337.mycurriculumdesign.Utils.RefreshMyParkingSpace;
import com.ztf0337.mycurriculumdesign.Utils.TextDate;
import com.ztf0337.mycurriculumdesign.Utils.TimeConverters;
import com.ztf0337.mycurriculumdesign.adapter.RentedParkingSpaceAdapter;
import com.ztf0337.mycurriculumdesign.entity.ParkingSpace;
import com.ztf0337.mycurriculumdesign.entity.RentedParkingSpace;
import com.ztf0337.mycurriculumdesign.service.RentService;
import com.ztf0337.mycurriculumdesign.ui.activity.MyRentDetailActivity;
import com.ztf0337.mycurriculumdesign.ui.activity.ParkingDetailActivity;
import com.ztf0337.mycurriculumdesign.ui.activity.SettleAccountsActivity;

import java.util.List;

public class MyParkingSpaceFragment extends Fragment {

    private RecyclerView rvRentedParkingSpace;
    private List<RentedParkingSpace> rentedParkingSpaceList;
    private RentedParkingSpaceAdapter rentedParkingSpaceAdapter;
    private RefreshMyParkingSpace refresher;
    private Context context;
    private ImageView ic_more,ic_filter,ic_delete,ic_affirm,ic_cansel,ic_Search;
    private RentService rentService;
    private EditText etSearch;

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {

        View view = inflater.inflate(R.layout.fragment_rented_parking_space, container, false);
        context = getContext();
        if (context == null) {
            return view;
        }
        rvRentedParkingSpace = view.findViewById(R.id.rvRentParkingSpace);
        rvRentedParkingSpace.setLayoutManager(new LinearLayoutManager(getContext()));
        SharedPreferences loginInfo = context.getSharedPreferences("loginInfo", Context.MODE_PRIVATE);
        rentService = new RentService(context);

        TextView notRent = view.findViewById(R.id.notRent);

        LinearLayout searchLayout = requireActivity().findViewById(R.id.search);
        searchLayout.setVisibility(GONE);

        if (!loginInfo.getBoolean("login",false)) {
            notRent.setText("未登录");
            notRent.setVisibility(VISIBLE);
            refresher = new RefreshMyParkingSpace(() -> {
                requireActivity().runOnUiThread(() -> {});
            });
        } else {
            rentedParkingSpaceList = rentService.getRentsByUserId(loginInfo.getLong("user_id",-1L));
            if (!rentedParkingSpaceList.isEmpty()) {
                notRent.setVisibility(GONE);
                searchLayout.setVisibility(VISIBLE);
                rentedParkingSpaceAdapter = new RentedParkingSpaceAdapter(rentedParkingSpaceList);
                rvRentedParkingSpace.setAdapter(rentedParkingSpaceAdapter);
                rentedParkingSpaceAdapter.setOnItemClickListener((rentedParkingSpace) -> {
                    Intent intent = new Intent(context, MyRentDetailActivity.class);
                    intent.putExtra("id", rentedParkingSpace.getId());
                    context.startActivity(intent);
                });
                refresher = new RefreshMyParkingSpace(() -> {
                    requireActivity().runOnUiThread(() -> {
                        rentedParkingSpaceAdapter.updateData(rentedParkingSpaceList);
                    });
                });
            } else {
                notRent.setText("无租借信息");
                notRent.setVisibility(VISIBLE);
                refresher = new RefreshMyParkingSpace(() -> {
                    requireActivity().runOnUiThread(() -> {});
                });
            }
        }


        etSearch = requireActivity().findViewById(R.id.etSearch);
        etSearch.setText(""); // 清空内容
        ic_Search = requireActivity().findViewById(R.id.ic_Search);

        etSearch.setHint("请输入车位号");
        ic_Search.setOnClickListener(v -> {
            String search = etSearch.getText().toString().trim();
            if (search.isBlank()) {
                if (rentedParkingSpaceList.isEmpty()) {
                    Toast.makeText(context, "无租借信息", Toast.LENGTH_SHORT).show();
                } else {
                    rentedParkingSpaceAdapter.updateData(rentedParkingSpaceList);
                }
                return;
            }
            List<RentedParkingSpace> rentedParkingSpaces = rentService.searchRents(search,loginInfo.getLong("user_id",-1L));
            if (rentedParkingSpaces.isEmpty()) {
                Toast.makeText(context, "无符合条件的租借", Toast.LENGTH_SHORT).show();
            } else {
                rentedParkingSpaceAdapter.updateData(rentedParkingSpaces);
            }
        });


        ic_filter = requireActivity().findViewById(R.id.ic_filter);
        ic_filter.setVisibility(GONE);
        ic_delete = requireActivity().findViewById(R.id.ic_delete);
        ic_delete.setVisibility(GONE);
        ic_affirm = requireActivity().findViewById(R.id.ic_affirm);
        ic_affirm.setVisibility(GONE);
        ic_cansel = requireActivity().findViewById(R.id.ic_cansel);
        ic_cansel.setVisibility(GONE);
        ic_more = requireActivity().findViewById(R.id.ic_more);
        ic_more.setVisibility(GONE);


        return view;
    }
    @Override
    public void onResume() {
        super.onResume();
        refresher.startRepeatingTask(); // 进入时开始定时任务
    }

    @Override
    public void onPause() {
        super.onPause();
        refresher.stopRepeatingTask(); // 离开时关闭任务
    }
}
